package models

import (
	"github.com/jinzhu/gorm"
	"kop/modules/maid/entity"
)

func GetIntimacy(dB *gorm.DB, roleID, serverID int32) entity.Intimacy {

	var data = entity.Intimacy{
		RoleID:   roleID,
		ServerID: serverID,
	}

	if err := dB.FirstOrCreate(&data).Error; err != nil {
		panic(err)
	}

	if data.ServerID != serverID {
		data.ServerID = serverID
	}

	return data
}

func UpdateIntimacy(dB *gorm.DB, roleID, serverID int32) {

	var n = struct {
		N int32
	}{}

	dB.Model(&entity.Maid{}).Where("role_id = ? AND is_friend = ?", roleID, true).Select("SUM(intimacy) AS n").Scan(&n)

	var data = GetIntimacy(dB, roleID, serverID)
	data.Intimacy = n.N
	dB.Save(&data)
}
